[% setvar title Timers and Timeouts %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Timers and Timeouts</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Uri Guttman &lt;<a href='mailto:uri@sysarch.com'>uri@sysarch.com</a>&gt;
  Date: 10 Aug 2000
  Mailing List: <a href='mailto:perl6-language-flow@perl.org'>perl6-language-flow@perl.org</a>
  Number: 87
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This RFC will discuss supporting multiple independent timers and
timeouts for asynchronous I/O events and exiting eval blocks.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Currently the only asynchronous timer mechanism in Perl is the alarm
signal. It has many flaws including being delivered unsafely and that
there is only one of them (which precludes nesting calls with their own
timeouts).</p>
<p>Under the asynchronous I/O (AIO) system all calls which support a
callback can also have an optional timeout. This event has a simple
common set of attribute/value arguments that can be used with any AIO
call. When no activity occurs on that AIO object before the timeout
period ends, the timeout is triggered and a callback is made. When any
activity occurs, the timeout is reset to its initial period (which can
also be done with the timeout_reset method). Standalone timeouts with no
AIO object can be created for use as independent timers.</p>
<p>Timers can be used to break out of any AIO operation and even out of
eval blocks by having the callback execute die (or quit or whatever). So
you can nest eval blocks and have control over how long each level takes
to run. Also this means no more collisions with sharing alarm calls.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>There are no syntactical changes needed for timeouts. All AIO calls will
be able to handle these additional optional arguments:</p>
<pre>	timeout		=&gt;	&lt;number in seconds. float is OK&gt;
	timeout_cb	=&gt;	&lt;timeout callback. see callback RFC&gt;
	timeout_method	=&gt;	&lt;method to call other then the default&gt;
	timeout_repeat	=&gt;	&lt;times to repeat&gt;</pre>
<p>The timeout arguments is in seconds but a float value can be used to get
fractions. The callback argument is the same as all the others in the
AIO system, it can be a coderef, an object or a class name. If you pass
in a timeout_method, that will be used as the callback. If the repeat
argument is passed the timeout will repeat that many times and forever
with the value -1.</p>
<p>Here is a typical AIO call with timeouts:</p>
<p>package Foo ;
$aio-&gt;read_event( cb =&gt; 'Foo', timeout =&gt; 5 ) ;</p>
<p>sub READABLE {</p>
<pre>	my( $self ) = @_ ;

	my $cnt = $aio-&gt;read( $buffer, 1024 ) ;
}</pre>
<p>sub READ_EVENT_TIMEOUT {</p>
<pre>	print &quot;nothing to read\n&quot; ;
}</pre>
<p>The default callback name for timeouts will be the AIO operation name
in upper case and _TIMEOUT appended to it. So the default methods for
read and connect are READ_TIMEOUT and CONNECT_TIMEOUT</p>
<p>Timers not associated with an AIO object can be created firectly with
this call:</p>
<pre>	$timer = AIO::Timer-&gt;new( timeout =&gt; 5, repeat =&gt; -1,
				  cb =&gt; sub{ print &quot;annoying, isn't it!\n&quot; }
	) ;</pre>
<a name='IMPACT'></a><h1>IMPACT</h1>
<p>It has to be emphasized that all timing in this RFC is a minimum
time. There is no guarantee for any speed of delivery of the
event. Supporting event priorities is a possibility to improve the
delivery speed some.</p>
<p>This requires one of the AIO delivery methods (event loops, mailboxes,
in line) to be enabled. See the use aio RFC.</p>
<a name='UNKNOWNS'></a><h1>UNKNOWNS</h1>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>Event.pm		- XS based event loop module.</p>
<p>RFC #47			- Universal Asynchronous I/O (the moby one)</p>
</div>
